package cn.tj.wenda.cloud.security;

import java.util.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class SHA256 {
    public static String getSignature(String claim,String secretKey) throws NoSuchAlgorithmException, InvalidKeyException, IllegalStateException, UnsupportedEncodingException {
        Mac sha256HMAC = Mac.getInstance("HmacSHA256");
        Base64.Encoder encoder = Base64.getEncoder();
        SecretKeySpec secret = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
        sha256HMAC.init(secret);
        String sha256 =  encoder.encodeToString(sha256HMAC.doFinal(claim.getBytes("UTF-8")));
        return sha256;
    }
}
